/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.reportlosebarcode.service
 * FILE    NAME: ReportLoseBarcodeDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.api.reportlosebarcode.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.hero.core.constant.SysConstant;
import com.hero.core.entity.PageResult;
import com.hero.core.entity.ResponseEntity;
import com.hero.core.exception.AbstractGenericException;
import com.hero.common.web.excel.util.ExcelExport;
import com.hero.common.web.excel.IExcelImportData;
import com.hero.core.util.DateUtils;
import com.hero.core.util.StringUtils;
import com.hero.core.util.ValidationUtil;
import com.jodinfo.outmirror.api.reportlosebarcode.entity.ReportLoseBarcodeEntity;

@Service(value="reportLoseBarcodeBizService")
public class ReportLoseBarcodeBizServiceImpl implements ReportLoseBarcodeBizService {
    private static final Logger logger = LoggerFactory.getLogger(ReportLoseBarcodeBizServiceImpl.class);
 	
 	@Resource(name="reportLoseBarcodeDmlService")
	private ReportLoseBarcodeDmlService ReportLoseBarcodeDmlService;
	@Resource(name="reportLoseBarcodeQueryService")
	private ReportLoseBarcodeQueryService ReportLoseBarcodeQueryService;
	
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeBizService#addReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param ReportLoseBarcodeEntity ReportLoseBarcodeEntity
	 * @return int
	 */
	public ResponseEntity addReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity) {
			ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
			  	 int i =  ReportLoseBarcodeDmlService.addReportLoseBarcode(ReportLoseBarcodeEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeBizService#updateReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param ReportLoseBarcodeEntity ReportLoseBarcodeEntity
	 * @return ResponseEntity
	 */
	public ResponseEntity updateReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = ReportLoseBarcodeDmlService.updateReportLoseBarcode(ReportLoseBarcodeEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeBizService#queryAllReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param ReportLoseBarcodeEntity ReportLoseBarcodeEntity
	 * @return String
	 */
	public String queryAllReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity) {
		return JSON.toJSONString(ReportLoseBarcodeQueryService.queryAllReportLoseBarcode(ReportLoseBarcodeEntity));
	} 
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeBizService#queryAllReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param ReportLoseBarcodeEntity ReportLoseBarcodeEntity
	 * @return PageResult
	 */
	public String queryListForPageReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity) {
		return new PageResult( ReportLoseBarcodeQueryService.queryListForPageReportLoseBarcode(ReportLoseBarcodeEntity), ReportLoseBarcodeQueryService.queryCountReportLoseBarcode(ReportLoseBarcodeEntity)).toString();
	} 
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeQueryService#queryById(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param BigInteger id
	 * @return
	 */
	public ReportLoseBarcodeEntity queryById(BigInteger id) {
		return ReportLoseBarcodeQueryService.queryById(id);
	}
	
	public void exportReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity, HttpServletResponse response) {
		List<ReportLoseBarcodeEntity> exportList = null;
		String ids = ReportLoseBarcodeEntity.getIds();
		if (ValidationUtil.isNullOrEmptyString(ids)) {
			exportList =ReportLoseBarcodeQueryService.queryAllReportLoseBarcode(ReportLoseBarcodeEntity);
		} else {
			String[] idArray = ids.split(",");
			List<BigInteger> idList = new ArrayList<BigInteger>();
			for(String id : idArray) {
				idList.add(new BigInteger(id));
			}
			exportList = ReportLoseBarcodeQueryService.queryByIds(idList);
			// 将字典中的code转换为name
		   //convertCodeToName(exportList);
		}
		ExcelExport.exportExcel(exportList, "ReportLoseBarcodeEntityExcel", "ReportLoseBarcodeEntity信息", response);
	}
	
	/** 
	 * 批量删除 数据处理
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeQueryService#queryById(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param BigInteger id
	 * @return
	 */
	public ResponseEntity batchDelReportLoseBarcode(ReportLoseBarcodeEntity ReportLoseBarcodeEntity) {
		String ids = ReportLoseBarcodeEntity.getIds();
		String[] idArray = ids.split(",");
		List<ReportLoseBarcodeEntity> ReportLoseBarcodeEntityList = new ArrayList<ReportLoseBarcodeEntity>();
		for(String id : idArray) {
			ReportLoseBarcodeEntity ReportLoseBarcodeModel = new ReportLoseBarcodeEntity();
			ReportLoseBarcodeModel.setId(new BigInteger(id));
//			ReportLoseBarcodeModel.setLastUpdateUsrName(UserContext.getUser().getName());
//			ReportLoseBarcodeModel.setLastUpdateTime(DateUtils.getCurrentTime());
//			ReportLoseBarcodeModel.setIsDel(SysConstant.IS_DEL_Y);
			ReportLoseBarcodeModel.setUpdateTime(DateUtils.getCurrentTime());
			ReportLoseBarcodeEntityList.add(ReportLoseBarcodeModel);
		}
		return batchUpdateReportLoseBarcode(ReportLoseBarcodeEntityList);
	}
	
	/**
	 * 执行批量删除操作
	 * @author weiwei
	 * @date 2017年3月13日 上午11:40:31
	 * @param ReportLoseBarcodeList
	 * @return
	 */
	public ResponseEntity batchUpdateReportLoseBarcode(List<ReportLoseBarcodeEntity> ReportLoseBarcodeList) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = ReportLoseBarcodeDmlService.batchUpdateReportLoseBarcode(ReportLoseBarcodeList);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	
	/**
	 * 
	 * @author weiwei
	 * @date 2017年3月13日 上午10:24:53
	 * @see  com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeQueryService#queryById(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param map
	 * @param excelimportcontext
	 * @param s
	 * @return
	 * @throws Exception
	 */
	/*public List<BigInteger> insertData(Map<Integer, List<Object>> map,ExcelImportContext excelimportcontext, String s) throws Exception {
		String usrName = UserContext.getUser().getName();//数据创建人
		String creatTime = DateUtils.getCurrentTime();
		List<ReportLoseBarcodeEntity> ReportLoseBarcodeList = validateReportLoseBarcodeData(map, excelimportcontext,usrName,creatTime);
		if (!excelimportcontext.hasError() && ReportLoseBarcodeList.size() > 0) {
			batchAddReportLoseBarcode(ReportLoseBarcodeList);
		}
		return null;
	}*/
	
	/**
	 * 
	 * TODO 验证数据 处理业务数据
	 * @author weiwei
	 * @date 2017年3月13日 上午9:33:30
	 * @see  com.jodinfo.outmirror.implementation.reportlosebarcode.ReportLoseBarcodeQueryService#queryById(ReportLoseBarcodeEntity ReportLoseBarcodeEntity)
	 * @param ReportLoseBarcodeList
	 * @return
	 */
	/*public List<ReportLoseBarcodeEntity> validateReportLoseBarcodeData(Map<Integer, List<Object>> map,ExcelImportContext excelimportcontext,String usrName,String creatTime) {
		ExcelImportContext.DataError errorMsg = null;
		List<ReportLoseBarcodeEntity> ReportLoseBarcodeList = new ArrayList<ReportLoseBarcodeEntity>();
		for (Map.Entry<Integer, List<Object>> entity : map.entrySet()) {
			List<Object> excelModelList = entity.getValue();
			if (ValidationUtil.isNullOrEmptyCollection(excelModelList)) {
		//		errorMsg = excelimportcontext.new DataError(InventoryConstantUtil.IMPORT_DATA_ERROR_TYPE_NOT_SHEET,
		//				"导入数据为空！",InventoryConstantUtil.IMPORT_DATA_ERROR_TYPE_NOT_SHEET);
		//		excelimportcontext.push(errorMsg);
			}else{
				//TODO 验证对应数据
				for (int i =0;i<excelModelList.size();i++) {
					ReportLoseBarcodeEntity ReportLoseBarcodeModel = (ReportLoseBarcodeEntity) excelModelList.get(i);
	 				if (checkDataByEntityModel(shShopModel, excelimportcontext, i)) {
						break;
					}
					//TODO 实体对象加入对应参数值 
					ReportLoseBarcodeModel = setEntityData(ReportLoseBarcodeModel,usrName, creatTime);
					ReportLoseBarcodeList.add(ReportLoseBarcodeModel);
				}
			}
		}
		return ReportLoseBarcodeList;
	}*/
	
	/**
	 * 
	 * 单个导入实体数据校验 有错误返回true
	 * @author weiwei
	 * @date 2017年3月28日 下午3:59:17
	 * @param eqEquipmentModel
	 * @param excelimportcontext
	 * @param i
	 * @return
	 */
	/*public boolean checkDataByEntityModel(ReportLoseBarcodeEntity ReportLoseBarcodeModel,ExcelImportContext excelimportcontext,int i) {
		if (checkSingleData(excelimportcontext, ReportLoseBarcodeModel.getIsDel, i+2,"错误信息") 
			//	|| checkSingleData(excelimportcontext, ReportLoseBarcodeModel.getEquipmentColor(), i+2, "颜色为空！")
			//	||checkSingleData(excelimportcontext, ReportLoseBarcodeModel.getEquipmentModel(), i+2, "型号为空！")
				) {
			return true;
		}else{
			return false;
		}
	}*/
	
	/**
	 * 
	 * 设置报错信息
	 * @author weiwei
	 * @date 2017年3月22日 下午5:17:37
	 * @param excelimportcontext
	 * @param rowNumId
	 * @param errorMessage
	 * @param sheetNumId 现在只有一个sheet 就不传值 默认值即可
	 */
	/*public boolean checkSingleData(ExcelImportContext excelimportcontext,String checkVal,int rowNumId,String errorMessage) {
		boolean flag = false;
		ExcelImportContext.DataError errorMsg = null;
		if (ValidationUtil.isNullOrEmptyString(checkVal)) {
			errorMsg = excelimportcontext.new DataError(rowNumId,errorMessage,InventoryConstantUtil.IMPORT_DATA_ERROR_TYPE_NOT_SHEET);
			excelimportcontext.push(errorMsg);
			flag = true;
		}
		return flag;
	}*/
	
	/**
	 * 
	 * 导入单个实体添加统一导入人及导入时间
	 * @author zhuxiaoliu
	 * @date 2017年3月28日 下午4:05:15
	 * @param eqEquipmentModel
	 * @param usrName
	 * @param creatTime
	 * @return
	 */
	/*public ReportLoseBarcodeEntity setEntityData(ReportLoseBarcodeEntity ReportLoseBarcodeModel,String usrName,String creatTime) {
		
		//ReportLoseBarcodeModel.setEquipmentId(InventoryConstantUtil.EQ_EQUIPMENT_SEQUENCE);
		ReportLoseBarcodeModel.setCreateTime(creatTime);
		ReportLoseBarcodeModel.setCreateUsrName(usrName);
		ReportLoseBarcodeModel.setLastUpdateUsrName(usrName);
		ReportLoseBarcodeModel.setLastUpdateTime(creatTime);
		ReportLoseBarcodeModel.setIsDel(SysConstant.IS_DEL_N);
		return ReportLoseBarcodeModel;
	}*/
	
	
	/**
	 * 
	 * 执行批插入操作
	 * @author weiwei
	 * @date 2017年3月13日 上午11:37:14
	 * @param {moduleName}List
	 * @return
	 */
	public ResponseEntity batchAddReportLoseBarcode(List<ReportLoseBarcodeEntity> ReportLoseBarcodeList) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
		try{
			int i = ReportLoseBarcodeDmlService.batchAddReportLoseBarcode(ReportLoseBarcodeList);
			 rs.setMessage(i+"条记录插入操作成功");
		 }catch(AbstractGenericException e){
			logger.error(e.getFullMessage());
			rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
		}catch(Exception e){
			String errorMessage = StringUtils.toString(e);
			logger.error(errorMessage);
			rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
		}
		return rs;
	}
}

